var express = require('express');
var Promise = require('promise');
var keep = require('../db/keep');
var fans = require('../db/fans');
var router = express.Router();

/* GET users listing. */
router.get('/:id*', function(req, res, next) {
	req.users.findById(req.params.id, function(err, result) {
		res.locals.upUser=result;
		res.locals.user = req.infos;
		fans.existFans(req,req.params.id,req.infos._id,function(d) {
			
			if (d.length>0) {

				res.locals.isFans = "true";
			} else {
				res.locals.isFans = "false";
			}
			return next();
		})
		// res.render('userhome',{'title':'个人中心','user':req.infos,'upUser':result,});
	});

});

router.get('/:id', function(req, res, next) {
	new Promise(function(resolve, reject){
		req.VandB.find({'upUser':req.params.id,'type':"article",pass:true}).sort({'upTime':1}).limit(6).toArray(function(err,items) {
			if (err) {
			reject(err);
			}
			resolve(items);
		});
	}).then(function(article){

		req.VandB.find({'upUser':req.params.id,'type':"video",pass:true}).sort({'upTime':1}).limit(6).toArray(function(err,items) {
			res.render('userhome',{'title':'个人中心-主页',
							   'router':'/userhome/'+req.params.id+'/',
							   'article':article,
							   'video':items,
							   'count':0,
							   'page':0,});
		});
	
	});	

});


router.get('/:id/video',function(req,res,next) {

	new Promise(function(resolve, reject){
		req.VandB.count({'upUser':req.params.id,'type':"video",pass:true},function(err,result){
			if (err) {
			reject(err);
			}
			resolve(result);
		});
	}).then(function(count) {
			if(!req.query.page || req.query.page==""){
				var	page = 1;
			} else {
				page = req.query.page;
			}

			skip = (page-1)*16;
			req.VandB.find({'upUser':req.params.id,'type':"video",pass:true})
			.sort({'upTime':1}).skip(skip).limit(16).toArray(function(err,items) {
				res.render('userhome',{
									 'title':'个人中心-视频',
									 'VA':items,
									 'count':count,
									 'page':page,
									 'router':'/userhome/'+req.params.id+'/video'});
			});
	});
});


router.get('/:id/article',function(req,res,next) {

	new Promise(function(resolve, reject){
		req.VandB.count({'upUser':req.params.id,'type':"article",pass:true},function(err,result){
			if (err) {
			reject(err);
			}
			resolve(result);
		});
	}).then(function(count) {
			if(!req.query.page || req.query.page==""){
				var	page = 1;
			} else {
				page = req.query.page;
			}

			skip = (page-1)*16;
			req.VandB.find({'upUser':req.params.id,'type':"article",pass:true})
			.sort({'upTime':1}).skip(skip).limit(16).toArray(function(err,items) {
				res.render('userhome',{ 'title':'个人中心-博文',
										'VA':items,
										'count':count,
										'page':page,
										'router':'/userhome/'+req.params.id+'/article'});
			});
	});
});


router.get('/:id/fans',function(req,res,next) {
	fans.findFans(req,req.params.id,function(d){
		var FIDS = [];
		if (d.Fans) {
			FIDS = d.Fans;
		}
		new Promise(function(resolve, reject){
		req.users.count({"_id":{$in:FIDS}},function(err,result){
			if (err) {
			reject(err);
			}
			resolve(result);
		});
		}).then(function(count){
			if(!req.query.page || req.query.page==""){
				var	page = 1;
			} else {
				page = req.query.page;
			}
			skip = (page-1)*16;
			req.users.find({"_id":{$in:FIDS}}).skip(skip).limit(16).toArray(function(err, items){
					
					res.render('userhome',{
						'title':'个人中心-粉丝',
						'ups':items,
						'router':'/userhome/'+req.params.id+'/fans',
						'count':count,
						'page':page,
					});
			});
		});
	});
});

router.get('/:id/keep',function(req,res,next) {
	keep.findKeep(req,req.params.id,function(d){
		// console.log(d.keepAV);
		var VAIDS = [];
		if (d.keepAT &&  d.keepAV) {
			VAIDS = d.keepAV.concat(d.keepAT);
		} else {
			if (d.keepAT) {
				VAIDS = d.keepAT;
			} 
			if (d.keepAV) {
				VAIDS = d.keepAV;
			}
		}
		
		new Promise(function(resolve, reject){
			req.VandB.count({"_id":{$in:VAIDS}},function(err,result){
			if (err) {
			reject(err);
			}
			resolve(result);
			});
		}).then(function(count) {
			if(!req.query.page || req.query.page==""){
				var	page = 1;
			} else {
				page = req.query.page;
			}

			skip = (page-1)*16;
			req.VandB.find({"_id":{$in:VAIDS}}).sort({'upTime':1})
			.skip(skip).limit(16).toArray(function(err,items) {
				res.render('userhome',{ 'title':'个人中心-收藏',
										'VA':items,
										'count':count,
										'page':page,
										'router':'/userhome/'+req.params.id+'/keep'});
			});
		});
	});
});

router.post('/delkeep',function(req,res,next) {
	keep.removeKeep(req,req.body.mydelete.name,
					req.body.mydelete.type,req.body.mydelete.kID,
					function(d){
						res.send(d);
					});
});


router.post('/addfans',function(req,res,next) {

	try{
		if (!req.infos.name) {
			res.status(404).render('404',{'err':'please login'})
		}else{
		fans.insertFans(req,req.body.addfans.name,req.body.addfans.FID,
						function(d){
							res.send(d);
						});
		}
	}catch(e){
		res.status(404).render('404',{'err':e})
	}
});

router.post('/refans',function(req,res,next) {

	try{
		if (!req.infos.name) {
			res.status(404).render('404',{'err':'please login'})
		}else{
		fans.removeFans(req,req.body.addfans.name,req.body.addfans.FID,
						function(d){
							res.send(d);
						});
		}
	}catch(e){
		res.status(404).render('404',{'err':e})
	}

});

router.post('/cgtheme',function(req,res,next) {

	req.users.updateById(req.infos._id,{$set:req.body.theme},function(err, result){
		if (err) {
			res.send('users db err');
		} else {
			res.send('ok');
		}
	});
	
});


router.post('/notice',function(req,res,next) {
	req.users.updateById(req.infos._id,{$set:req.body.notice},function(err, result){
		if (err) {
			res.send('users db err');
		} else {
			res.send('ok');
		}
	});
});


module.exports = router;
